home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14786 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news-relay.eworld.com!zdc!zippo!drn
  2. From: Clarence Chiang
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Doubly Linked List help - please!
  5. Date: 1 Apr 1996 12:16:00 -0800
  6. Organization: Zippo
  7. Sender: http@doc.zippo.com
  8. Message-ID: <4jpdi0$360@doc.zippo.com>
  9. NNTP-Posting-Host: doorstop.spiderisland.com
  10.  
  11. In article <internews46B70D427A@argonet.co.uk>, Charlotte says...
  12. >
  13. >I have implemented a template doubly linked list, the backbones of which
  14. >is as follows:
  15. >
  16. >//dllist.h
  17. >
  18. >/*snip*/
  19. >
  20. >template <class T> class node
  21. >{
  22. >        public:
  23. >        T data;
  24. >        node<T> *next;
  25. >        node<T> *prev;
  26. >        node();
  27. >        node(T info);
  28. >        node<T> *getnext() const {return next;}
  29. >        node<T> *getprev() const {return prev;}
  30. >        T getinfo() const {return data;}
  31. >        void change(T info) {data=info;}
  32. >/*other functions*/
  33. >};
  34. >
  35. >template <class T> class dllist : public node<T>
  36. >{
  37. >        node<T> *start, *end;
  38. >        void copy(const dllist<T>);
  39. >
  40. >        public:
  41. >        dllist() {start=end=NULL;}
  42. >        dllist(const dllist<T>&);
  43. >        int isEmpty() const;
  44. >/*store, remove, find, ==, etc. snipped*/
  45. >};
  46. >
  47. >
  48. >Then I have a class, called fuzzyset, which uses this dll as a container
  49. >for fuzzyel instances:
  50. >
  51. >//fuzzyset.h
  52. >/*snip*/
  53. >
  54. >class fuzzyset {
  55. >        private:
  56. >        dllist<fuzzyel> members;
  57. >
  58. >        public:
  59. >        fuzzyset() {}
  60. >        fuzzyset(const fuzzyset&);
  61. >        fuzzyset(const fuzzyel&);
  62. >/*other functions and operators*/
  63. >};
  64. >
  65. >
  66. >What I would like to do is, within dllist, to implement a function forEach
  67. >that take a pointer to a function as one argument, and somehow the rest of
  68. >the arguments required by that function, and carries that function out
  69. >over each element of the list. 
  70. >
  71. >One example of what I need to do is as follows.  In my main program (where
  72. >fuzzyset is included), I need to be able to call a function that prints
  73. >every element of the 'members' list in any called instance of a fuzzyset.
  74. >
  75. >I need the forEach function to be quite generic, because I will have other
  76. >uses for it later.
  77. >
  78. >I hope someone can help.  Many thanks in advance.
  79. >
  80. >
  81. >
  82. >
  83. >
  84. >... So, how did I do?
  85. >-- 
  86. >-----------------------------------------------------------------------------
  87. >| Charlotte Tomlinson  |    Mediocrity know nothing higher than itself,     |
  88. >| eeyore@argonet.co.uk |       but talent instantly recognises genius.      |
  89. >|---------------------------------------------------------------------------|
  90. >--------- Now WWWebbed up at http://www.argonet.co.uk/users/eeyore/ ---------
  91. >
  92.  
  93. Why don't you implement an iterator class that's friend class of dllList ? In
  94. that way you don't have to expose anything (data member or tranversal member
  95. functions) and still be able to apply functions to each element.
  96.  
  97. If you look into the STL, there will be a lot of examples of how to write and use
  98. iterators.
  99.  
  100. Clarence Chiang
  101. Spider Island Software
  102.  
  103.